package keeto.com.vn.bms.ui.adapter;

import java.util.ArrayList;
import keeto.com.vn.bms.model.BaseInfor.ProductInfor;
import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.utils.UniversalImageLoader;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListProductAdapter extends BaseAdapter {
	private Context mContext;
	private ArrayList<ProductInfor> products;
	private LayoutInflater inflater;
	private UniversalImageLoader iLoader;

	public ListProductAdapter(Context context, ArrayList<ProductInfor> product) {
		super();
		this.mContext = context;
		this.products = product;
		this.inflater = LayoutInflater.from(context);
		this.iLoader= new UniversalImageLoader(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_list_infor_fragment,parent, false);
			holder = new ViewHolder();
			holder.imgProduct = (ImageView) convertView
					.findViewById(R.id.img_lv_product);
			holder.tvName = (TextView) convertView
					.findViewById(R.id.tv_name_product);
			holder.tvDesc = (TextView) convertView
					.findViewById(R.id.tv_des_product);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (!products.get(position).getImage().equals("")) {
			iLoader.displayImageView(products.get(position).getImage(),
					holder.imgProduct);
		}else{
			holder.imgProduct.setBackgroundResource(R.drawable.no_image);
		}
		holder.tvName.setText(products.get(position).getTitle());
		holder.tvDesc.setText(products.get(position).getIntro());

		return convertView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return products.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return products.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	private class ViewHolder {
		TextView tvName;
		TextView tvDesc;
		ImageView imgProduct;
	}
}
